package com.zhida.smartcontrol.pop;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jay.tools.base.BasePopWindow;
import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.DimenUtils;
import com.jay.tools.utils.ResUtil;
import com.zhida.smartcontrol.R;
import com.zhida.smartcontrol.common.bean.LampBean;

import java.util.ArrayList;
import java.util.List;

/**
 * author: tanjianpei
 * date: 2019/10/10 010 10:42
 * description: 集控器
 */
public class JkqPop extends BasePopWindow implements View.OnClickListener {
    private FrameLayout mFlOpen;
    private TextView mTvOpen;
    private FrameLayout mFlClose;
    private TextView mTvClose;
    private LinearLayout mLBtnGroup;
    private TextView mTvSwitchName;
    private TextView mTvQuduan;
    private TextView mTvTime;
    private TextView mTvArea;
    private TextView mTvAddressPoint;
    private TextView mTvAddress;
    private ButtonStyle mBtnOnline;
    private TextView mTvDy;
    private TextView mTvDliu;
    private TextView mTvGlv;
    private ImageView mIvTd0, mIvTd1, mIvTd2, mIvTd3, mIvTd4, mIvTd5, mIvTd6, mIvTd7;
    private List<ImageView> mIvList;
    private ButtonStyle mBtnAuto;
    private ButtonStyle mBtnHand;
    private ButtonStyle mBtnSetTime;
    private LinearLayout mLLJkq;

    private List<String> mCodeList;

    public JkqPop(Context context) {
        super(context);
        setWidth(DimenUtils.dp2px(mContext, 345));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_home_jkq;
    }

    public void bindData(LampBean bean) {
        mCodeList.add(bean.getCode());

        mTvSwitchName.setText(bean.getSwitchTypeTxt());
        mTvQuduan.setText(bean.getGroupName());
        mTvTime.setText("设备更新时间：" + bean.getUpdateTime());
        mTvAddressPoint.setText(bean.getGroupName());
        mTvAddress.setText(bean.getAddress());
        // 0离线，1在线
        if (bean.getOnlineStatus().equals("0")) {
            mBtnOnline.setText("离线");
            mBtnOnline.setEnabled(false);
        } else if (bean.getOnlineStatus().equals("1")) {
            mBtnOnline.setText("在线");
            mBtnOnline.setEnabled(true);
        }

        setChannel(bean.getReturnValueTxt());

        String controlType = bean.getControlType();
        if (controlType.equals("0")) { //自动模式
            mBtnSetTime.setVisibility(View.VISIBLE);
            mLBtnGroup.setVisibility(View.GONE);
            mBtnAuto.setEnabled(false);
            mBtnHand.setEnabled(true);

            setHeight(DimenUtils.dp2px(mContext, 170));
        } else { // 手动
            mBtnHand.setEnabled(false);
            mBtnAuto.setEnabled(true);
            mBtnSetTime.setVisibility(View.GONE);
            mLBtnGroup.setVisibility(View.VISIBLE);

            setHeight(DimenUtils.dp2px(mContext, 215));
        }

        changeTdBtn(bean.getIsOpen().equals("1"));
    }

    // 设置集控器通道状态  // 0关闭， 1开启
    private void setChannel(String returnValueTxt) {
        int channelNum = 4;
        returnValueTxt = new StringBuffer(returnValueTxt).reverse().toString();
        String[] split = returnValueTxt.split("");
        int size = mIvList.size();
        for (int i = 0; i < size; i++) {
            ImageView iv = mIvList.get(i);
            if (i < channelNum) {
                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(getRes(split[i]));
            } else {
                iv.setVisibility(View.GONE);
            }
        }
    }

    private int getRes(String state) {
        if (state.equals("1")) { //1,开启
            return R.drawable.ic_td_open;
        } else {// if (state.equals("0")) { //0,关闭
            return R.drawable.ic_td_close;
        }
    }

    @Override
    protected void initViews(View view) {
        mBtnSetTime = view.findViewById(R.id.btn_set_time);
        mIvTd0 = view.findViewById(R.id.iv_td_0);
        mIvTd1 = view.findViewById(R.id.iv_td_1);
        mIvTd2 = view.findViewById(R.id.iv_td_2);
        mIvTd3 = view.findViewById(R.id.iv_td_3);
        mIvTd4 = view.findViewById(R.id.iv_td_4);
        mIvTd5 = view.findViewById(R.id.iv_td_5);
        mIvTd6 = view.findViewById(R.id.iv_td_6);
        mIvTd7 = view.findViewById(R.id.iv_td_7);

        mTvDy = view.findViewById(R.id.tv_dian_ya);
        mTvDliu = view.findViewById(R.id.tv_dian_liu);
        mTvGlv = view.findViewById(R.id.tv_gong_lv);
        mBtnOnline = view.findViewById(R.id.btn_online);
        mTvAddress = view.findViewById(R.id.tv_address);
        mTvAddressPoint = view.findViewById(R.id.tv_address_point);
        mTvSwitchName = view.findViewById(R.id.tv_switch_type);
        mTvQuduan = view.findViewById(R.id.tv_qu_duan);
        mTvTime = view.findViewById(R.id.tv_time);
        mTvArea = view.findViewById(R.id.tv_area);
        mBtnAuto = view.findViewById(R.id.btn_auto);
        mBtnHand = view.findViewById(R.id.btn_hand);
        mFlOpen = view.findViewById(R.id.fl_td_open);
        mTvOpen = view.findViewById(R.id.tv_td_open);
        mFlClose = view.findViewById(R.id.fl_td_close);
        mTvClose = view.findViewById(R.id.tv_td_close);
        mLBtnGroup = view.findViewById(R.id.ll_btn_group);
        mLLJkq=view.findViewById(R.id.ll_jkq);
    }

    @Override
    protected void initData() {
        mIvList = new ArrayList<>();
        mIvList.add(mIvTd0);
        mIvList.add(mIvTd1);
        mIvList.add(mIvTd2);
        mIvList.add(mIvTd3);
        mIvList.add(mIvTd4);
        mIvList.add(mIvTd5);
        mIvList.add(mIvTd6);
        mIvList.add(mIvTd7);

        mTvArea.setVisibility(View.GONE);
        mTvDy.setVisibility(View.GONE);
        mTvDliu.setVisibility(View.GONE);
        mTvGlv.setVisibility(View.GONE);

        mCodeList = new ArrayList<>();
        // 1、2两个参数表示左上角，3、4表示右上角，5、6表示右下角，7、8表示左下角
        float[] floatAuto = {20, 20, 0, 0, 0, 0, 20, 20};
        for (int i = 0; i < floatAuto.length; i++) {
            floatAuto[i] = DimenUtils.dp2px(mContext, floatAuto[i]);
        }
        mBtnAuto.setRadius(floatAuto);

        float[] floatHand = {0, 0, 20, 20, 20, 20, 0, 0};
        for (int i = 0; i < floatHand.length; i++) {
            floatHand[i] = DimenUtils.dp2px(mContext, floatHand[i]);
        }
        mBtnHand.setRadius(floatHand);
    }

    @Override
    protected void initEvents() {
        mBtnAuto.setOnClickListener(this);
        mBtnHand.setOnClickListener(this);
        mFlOpen.setOnClickListener(this);
        mFlClose.setOnClickListener(this);
        mBtnSetTime.setOnClickListener(this);
    }

    @Override
    protected int setCustomHeight() {
        return DimenUtils.dp2px(mContext, 215);
    }

    public void changeMode(int type) {
        // 0自动，1手动
        if (type == 0) {
            mBtnSetTime.setVisibility(View.VISIBLE);
            mLBtnGroup.setVisibility(View.GONE);
            mBtnAuto.setEnabled(false);
            mBtnHand.setEnabled(true);

            ViewGroup.LayoutParams layoutParams = mLLJkq.getLayoutParams();
            layoutParams.height=DimenUtils.dp2px(mContext, 170);
            mLLJkq.setLayoutParams(layoutParams);
        } else {
            mBtnHand.setEnabled(false);
            mBtnAuto.setEnabled(true);
            mBtnSetTime.setVisibility(View.GONE);
            mLBtnGroup.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams layoutParams = mLLJkq.getLayoutParams();
            layoutParams.height=DimenUtils.dp2px(mContext, 215);
            mLLJkq.setLayoutParams(layoutParams);
        }
    }

    @Override
    public void onClick(View v) {
        int tag = -1;
        switch (v.getId()) {
            case R.id.btn_auto:
                tag = 0;
                break;
            case R.id.btn_hand:
                tag = 1;
                break;
            case R.id.fl_td_close:
                tag = 2;
                break;
            case R.id.fl_td_open:
                tag = 3;
                break;
            case R.id.btn_set_time:
                tag=4;
                break;
        }
        if (null != mListener) {
            mListener.onClick(mCodeList, tag);
        }
    }

    /**
     * @param open 开启状态true, 关闭 false
     */
    public void changeTdBtn(boolean open) {
        mFlOpen.setEnabled(!open);
        mFlOpen.setBackgroundResource(open ? R.drawable.db_button_1_1 : R.drawable.db_button_2_1);
        mTvOpen.setTextColor(ResUtil.getColor(mContext, open ? R.color.c_FFFFFF : R.color.c_3862FF));
        mTvOpen.setBackgroundResource(open ? android.R.color.transparent : R.drawable.s_bg_search);

        mFlClose.setEnabled(open);
        mFlClose.setBackgroundResource(open ? R.drawable.db_button_2_1 : R.drawable.db_button_1_1);
        mTvClose.setTextColor(ResUtil.getColor(mContext, open ? R.color.c_3862FF : R.color.c_FFFFFF));
        mTvClose.setBackgroundResource(open ? R.drawable.s_bg_search : android.R.color.transparent);
    }


}
